package cn.edu.huanu.dao.impl;

import cn.edu.huanu.dao.UserDao;
import cn.edu.huanu.exception.DaoException;
import cn.edu.huanu.pojo.User;
import cn.edu.huanu.utils.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * @author hb
 * @date 2025-10-17 15:22:42
 */
public class UserDaoImpl implements UserDao {
    @Override
    public User queryUserByUsernameAndPwd(String username, String pwd) {
        final String sql="select id,username,password,doTime from t_user where username=? and password=md5(?)";
        User user=null;
        Connection conn =null;
        PreparedStatement pstm =null;
        ResultSet rs=null;
        try{
            conn=  ConnectionUtils.newConnection();
            pstm = conn.prepareStatement(sql);
            pstm.setString(1,username);
            pstm.setString(2,pwd);
            //rs为结果集：获得结果
            rs = pstm.executeQuery();
            if(rs.next()){//结果集中存在数据
                //根据字段名获得结果
                int id = rs.getInt("id");
                String name = rs.getString("username");
                String password = rs.getString("password");
                LocalDateTime doTime = rs.getTimestamp("doTime").toLocalDateTime();
                user = new User(id,name,password,doTime);
            }

            return user;


        }catch(SQLException e){
            throw new DaoException(e);
        } finally{
            ConnectionUtils.close(conn,pstm,rs);
        }
    }

    @Override
    public User queryUserByUserName(String username) {
        final String sql="select id,username,doTime from t_user where username=?";
        User user=null;
        Connection conn =null;
        PreparedStatement pstm =null;
        ResultSet rs=null;
        try{
            conn=  ConnectionUtils.newConnection();
            pstm = conn.prepareStatement(sql);
            pstm.setString(1,username);
            //rs为结果集：获得结果
            rs = pstm.executeQuery();
            if(rs.next()){//结果集中存在数据
                //根据字段名获得结果
                int id = rs.getInt("id");
                String name = rs.getString("username");
                user = new User();
                user.setId(id);
                user.setUsername(name);
            }

            return user;


        }catch(SQLException e){
            throw new DaoException(e);
        } finally{
            ConnectionUtils.close(conn,pstm,rs);
        }
    }

    @Override
    public void insertUser(User user) {
      //1.定义SQL语句
     final String sql="insert into t_user(username,password) values(?,md5(?))";
        Connection conn =null;
        PreparedStatement pstm =null;
        try {
            //2. 获得Connection对象
             conn = ConnectionUtils.newConnection();
              pstm = conn.prepareStatement(sql);
            pstm.setString(1,user.getUsername());
            pstm.setString(2,user.getPwd());
            //3. 执行SQL语句
            pstm.executeUpdate();
        } catch (SQLException e) {
            throw new DaoException(e.getMessage());
        }finally{
            //4. 关闭连接
            ConnectionUtils.close(conn,pstm);
        }

    }
}
